<?php
/**
 * Created by PhpStorm.
 * User: phc
 * Date: 19-8-14
 * Time: 下午4:59
 */

namespace app\admin\controller;


use app\common\model\SlideshowModel;
use think\Controller;

class SlideshowController extends Controller
{
    public function index()
    {
        return view();
    }

    public function page()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $item = new SlideshowModel();
            $data = $item->getSlideShows($post['limit'], $post['page'], false);
            $count = $item->getSlideShowCount();
            return pagejson($data, $count);
        }
    }

    public function add()
    {
        if (!$this->request->isAjax()) {
            $data['item'] = null;
            $data['cates'] = null;
            return view('form', $data);
        }
        $post = $this->request->post();
        $result = false;
        if ($post) {
            $slideshowModel = new SlideshowModel();
            $phy = $post["phy"];
            unset($post["phy"]);
            unset($post["file"]);
            $result = $slideshowModel->addSlideShow($post, $phy);
        }
        if ($result === false) {
            return zbcjson(false, '插入错误', 101);
        } else {
            return zbcjson(true);
        }
    }

    public function edit($id = 0)
    {
        $slideshowModel = new SlideshowModel();
        if (!$this->request->isAjax()) {
            $result = $slideshowModel->getSlideShowById($id);
            $data['item'] = $result["data"];
            $data["item"]["phy"] = '';
            if ($result["image"])
                $data["item"]["phy"] = $result["image"][0]["phy"];
            $data['cates'] = [];
            return view('form', $data);
        }
        $post = $this->request->post();
        $phy = $post["phy"];
        unset($post["phy"]);
        unset($post["file"]);
        $result = $slideshowModel->updateSlideShowById($post, $phy);
        if ($result === false) {
            return zbcjson(false, '修改错误', 101);
        } else
            return zbcjson(true);

    }

    public function delete()
    {
        $post = $this->request->post();
        $result = false;
        $data = $post['data'];
        $slideshowModel = new SlideshowModel();
        $result = $slideshowModel->deleteSlideshow($data);
        if ($result === false) {
            return zbcjson(false, '删除错误', 101);
        } else
            return zbcjson(true);
    }
}